<?php
/**
 * 新闻管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-31
 */
namespace Admin\Controller;

class NewsController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db   =new \Model\NewsModel();
        $this->com  =new \Model\NewsCommentModel();
    }
    
    /*
     * 新闻列表
     *  
     * @return #
     */
    public function index(){
        if(I("request.title")){
            $map['title']   =I("request.title");
        }
        if(I("request.description")){
            $map['description'] =I("request.description");
        }        
        $map=$this->_search("News");
        $map['state']   =1;
        $sortBy         ="time";
        $sort           =FALSE;
        $newsList       =$this->_list($this->db,$map,$sortBy,$sort);
        $this->assign("newsList",$newsList);
        $this->display();
    }  
    
    /*
     * 显示编辑页面
     * 
     * @return #
     */
    public function show(){
        if(I("request.sign")){
            $this->assign("sign",I("request.sign"));
        }
        if(I("request.id")){
            $newsinfo   =$this->db->find(I("request.id"));
            $this->assign("newInfo",$newsinfo);
        }
        $this->display();
    }
    
    /*
     * 新增，编辑
     * 
     * @return #
     */
    public function edit(){
        if(I("request.id")){
            $map['id']      =I("request.id");
        }
        if(I("request.title")){
            $map['title']   =I("request.title",'','trim'); 
        }
        if(I("request.fromWhere")){
            $map['fromWhere']=I("request.fromWhere");
        }
        if(I("request.author")){
            $map['author']  =I("request.author");
        }
        /*
        if(I("request.contentUrl")){
            $map['contentUrl']  =I("request.contentUrl");
        }
         * 
         */
        if(I("request.description")){
            $map['description'] =I("request.description",'','stripslashes');
        }
        if(I("request.top")){
            $map['top_sign']    =1;
        }
        if(I("request.icon")){
            $map['icon']        =I("request.icon");
        }
        $map['date']            =date("Y-m-d", time());       
        if($this->db->edit($map)){
            $id_con             =$this->db->where($map)->Field("id,description")->find();
            $maps['id']         =$id_con['id'];
            $maps['contentUrl'] ='http://112.74.30.49'.__APP__."/Home/News/index/p/".$maps['id'];
            
            if(strpos(trim($id_con['description']),"embed")){
                $embedstart     =strpos($id_con['description'],"<embed");
                $embed          =substr($id_con['description'],$embedstart);
                $embedend       =strpos($embed,">");
                $olddescription =substr($id_con['description'],$embedstart,(int)$embedstart+(int)$embedend+1);
                $embed_1        =substr($olddescription,strpos($olddescription,'src'),strpos($olddescription,".")+8);
                $embed_2        =substr($embed_1,0,strripos($embed_1,'"')+1);                
                $newdescription     ='<video '.$embed_2.' controls="controls" width="100%" preload="auto" autoplay="autoplay">
                 
                                       您的浏览器不支持video标签。
                                    </video>';
                $maps['description']= str_replace($olddescription,$newdescription, $id_con['description']);
                
            }
            $map['content'] =$maps['description']?$maps['description']:I("request.description",'','trim');
            $url            =A("Common")->makeHtml($map,$id_con['id']);
            $url            =substr($url,strripos($url,'/')-4);
            $this->db->save($maps);
            $this->success("新闻编辑成功");
        }else{
            $this->error("新闻编辑失败");
        }
    }
    /*
     * 删除指定新闻
     * 
     * @return #
     */
    public function del(){
        if(!I("request.id")){
            $this->error("参数错误");
        }else{
            //$map['title']   =$this->db->where(array("id"=>I("request.id")))->getField("title");
            $map=array(
                'id'    =>I("request.id"),
                'state' =>0
            );
            if($this->db->del($map)){
                $this->success("新闻删除成功");
            }else{
                $this->error("新闻删除失败");
            }
        }
    }
    
    /*
     * 新闻评论列表
     * 
     * @return #
     */
    public function newsComment(){        
        if(I("request.commentContent")){
            $map['commentContent']  =I("request.commentContent");
        }
        $map=$this->_search("NewsComment");
        if("request.title"){
            $map['newsId']          =A("Common")->getIds(I("request.title"),"title","News");
        }
        if(I("request.caedate")){
            $map['caedate']         =array("egt",I("request.caedate"));
        }
        if(I("request.newsId")){
            $map['newsId']      =I("request.newsId");
        }
        $map['state']   =2;
        $sortBy         ='commentId';
        $sort           =desc;
        $commentList    =$this->_list($this->com,$map,$sortBy,$sort,"commentId");
        $this->assign("commentList",$commentList);
        $this->assign('act',1);
        $this->display();
    }
    
    /*
     * 待审核评论
     * 
     * @return #
     */
    public function auditNews(){        
        if(I("request.commentContent")){
            $map['commentContent']  =I("request.commentContent");
        }
        $map=$this->_search("NewsComment");
        if("request.title"){
            $map['newsId']          =A("Common")->getIds(I("request.title"),"title","News");
        }
        if(I("request.caedate")){
            $map['caedate']         =array("egt",I("request.caedate"));
        }
        $map['state']   =1;
        $sortBy         ='commentId';
        $sort           =desc;
        $commentList    =$this->_list($this->com,$map,$sortBy,$sort,"commentId");
        $this->assign("commentList",$commentList);
        $this->assign('act',2);
        $this->display("newsComment");
    }
    
    /*
     * 审核，批量审核
     * 
     * @return #
     */    
    public function alledit(){       
        $map['commentId']   =I("request.id");
        $map['state']       =2;
        if($this->com->edit($map)){            
            $this->success("审核成功");
        }else{
            $this->error("审核失败");
        }
    }
    
    /*
     * 待审，批量待审
     * 
     * @return #
     */
    public function audit(){
        $map['commentId']   =I("request.id");
        $map['state']       =1;
        if($this->com->edit($map)){            
            $this->success("待审核成功");
        }else{
            $this->error("待审核失败");
        }
    }
    
    /*
     * 删除，批量删除 评论
     * 
     * @return #
     */
    public function delCom(){
        $map['commentId']   =I("request.id");
        $map['state']       =3;
        if($this->com->edit($map)){            
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    } 
    
    /*
     * 图片上传
     * 
     * @return #
     */
    public function newUp(){
        if(!empty($_FILES['images']['name'])){
            $config =array(
                'rootPath'  =>'./Application/Public/upload/',                              //根目录
                'savePath'  =>'new/',                                            //保存路径
                'maxSize'   =>'902400',                                             //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('jpg','png','gif','jpeg',"JPG","PNG","JPEG","GIF")    //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['images']);
            if(!$upload){
                echo $new_upload->getError();
            }else{                
                echo ($upload['savepath'].$upload['savename']);                              
            }           
        }
    } 
}